<template>
	<view>
		<!-- 头部导航栏 -->
		<view style="display: flex;justify-content: space-between;align-items: center;padding: 28rpx 20rpx;position:fixed;top:0;left:0;right:0;height: 60px;z-index: 22;background-color: white;">
			<view style="position: absolute;z-index: 22;display: flex;" @click="back">
				<u-icon name="arrow-left"></u-icon>
				<view>返回</view>
			</view>

			<view style="font-weight: bold;position: absolute;left: 0;right: 0;height: 100%;display: flex;align-items: center;justify-content: center;">
				<view style="margin-left: 10rpx;">
					消息模板管理
				</view>
			</view>
			
			<view @click="createNews" style="position: absolute;z-index: 22;right: 25rpx;;display: flex;align-items: center;;padding: 10rpx 25rpx;margin: 0 10rpx;border-radius: 10rpx;box-shadow: 0 0 14px 0 #66666635;border: solid 1rpx #ddd;">
				<view style="margin-left: 10rpx;">
					新建消息
				</view>
			</view>
		</view>
		
		<view style="margin: 60px 10px 10px 10px;padding-top: 10rpx;">
			<view v-for="(news,index) in news">
				<view style="border-radius: 5px;overflow: hidden;margin: 20rpx 0 70rpx 0;box-shadow: 0 0 19px 1px #00000045;">
					<view style="margin: 0 20rpx;">
						<view style="display:flex;align-items: center;justify-content:space-between;margin: 20rpx 5rpx 10rpx 5rpx;font-size: 13px;">
							
							<view style="display: flex;">
								<view style="color: #888;">{{'上次发表:' + $dayjs(news.created_at).format('MM-DD HH:mm') }}</view>
								<view style="color: #888;">{{`(${$dayjs(news.created_at).fromNow()})`}}</view>
								<!-- <view style="color: #888;margin-left: 20rpx;">{{'桔赞'}}</view> -->
							</view>
							<view @click="()=>{showDeleteComfirm=true;selectedNews=news}" style="display: flex;align-items: center;padding: 10rpx 25rpx;margin: 0 0rpx;border-radius: 10rpx;box-shadow: 0 0 14px 0 #66666635;border: solid 1rpx #ddd;">
								<u-icon name="close-circle-fill" color="#e70003"></u-icon>
								<view style="margin-left: 10rpx;">
									删除
								</view>
							</view> 
						</view>
						<view style="display:flex;align-items: flex-end;justify-content:space-between;margin: 10rpx 5rpx 20rpx 5rpx;font-size: 13px;">
							<view style="color: #888;">{{'创建日期:' + $dayjs(news.created_at).format('YYYY-MM-DD HH:mm')}}</view>
							<view style="color: #888;">{{'次数:' + 5}}</view>
						</view>
					</view>
					
					<view @click="onPost(news.posts[0])" style="height: 150px;overflow: hidden;display: flex;align-items: center;position: relative;">
						<image style="width: 100%;" mode="widthFix" :src="absUrl(news.posts[0].thumb_url)"></image>
						
						<view style="display: flex;align-items: center;;position: absolute;left: 0;right: 0;top: 60%;bottom: 0;background-image: linear-gradient(0deg,#212121cc,#00000000);padding: 14rpx;overflow: hidden;">
							<view style="color: white;font-weight: bold;font-size: 16px">
								{{news.posts[0].title}}
							</view>
						</view>
					</view>
					
					<template v-if="news.posts.length > 0" >
						<template v-for="(post,i) in news.posts">
							<view v-if="i> 0" @click="onPost(post)" style="display: flex;padding: 20rpx 20rpx;border-bottom: solid 1rpx #eee;">
								<view style="flex: 1;">
									<view style="font-weight: bold;font-size: 15px;padding-bottom: 10px;display: flex;justify-content: flex-start;align-items: center;height: 100%;">
										{{post.title}}
									</view>
									<!-- <view style="color: #888;">{{post.digest}}</view> -->
								</view>
								<view style="overflow: hidden;">
									<image style="width: 60px;height: 60px;" mode="aspectFill" :src="absUrl(post.thumb_url)"></image>
								</view>
							</view>
						</template>
					</template>
				</view>
			</view>
		</view>
	</view>
	
	<u-modal
		:show="showDeleteComfirm" 
		showCancelButton
		@close="showDeleteComfirm=false" 
		@cancel="showDeleteComfirm=false"
		@confirm="()=>{showDeleteComfirm = false;onDeleteComfirm(selectedNews)}"
		title="是否确认删除?"
	>
	</u-modal>
</template>

<script>
	export default {
		async onLoad(options){
			// let res = this.$localStorage.getItem('posts') 
			
			// if(!res)
			// {
			// 	 res = await this.$http.get('/common/wx/get_published_list')
			// 	 this.$localStorage.setItem('posts',res)
			// }
			
			// this.$officePost.drafts = res.data.item
			
			this.office_id = options.office_id
		},
		async onShow(){
			this.uploadList()
		},
		data() {
			return {
				news:[],
				showDeleteComfirm:false,
				selectedNews:null,
				office_id:0
			}
		},
		methods: {
			async uploadList(){
				let res = await this.$http.get(`/common/wx/template_news_list?office_id=${this.office_id}`)

				this.news = res.data
			},
			back(){
				uni.navigateBack()
			},
			onPost(posts){ 
				uni.navigateTo({
					url: `post-preview?id=${posts.id}`
				})
			},
			createNews(){
				uni.navigateTo({
					url: '/pages/job/publish/job-offices/news-manager/select-post-draft'
				})
			},
			async onDeleteComfirm(news){
				console.log(news)
				
				uni.showLoading({
					title:'正在删除'
				})
				
				await this.$http.post('/common/wx/delete_news',{
					news_id:news.id
				})
			
				uni.hideLoading()
				
				await this.uploadList()
				
				setTimeout(()=>{
					uni.showToast({
						title:'删除成功'
					})
				},500)
			},
			onDelete(news)
			{
				
			}
		}
	}
</script>

<style>

</style>
